{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "pip install -U skorch"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7QssE4BEh9Ui",
        "outputId": "2c56b385-0eff-4d61-f00e-4c218a3de6db"
      },
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Collecting skorch\n",
            "  Downloading skorch-0.11.0-py3-none-any.whl (155 kB)\n",
            "\u001b[K     |████████████████████████████████| 155 kB 27.9 MB/s \n",
            "\u001b[?25hRequirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python3.7/dist-packages (from skorch) (1.21.6)\n",
            "Requirement already satisfied: scikit-learn>=0.19.1 in /usr/local/lib/python3.7/dist-packages (from skorch) (1.0.2)\n",
            "Requirement already satisfied: tqdm>=4.14.0 in /usr/local/lib/python3.7/dist-packages (from skorch) (4.64.0)\n",
            "Requirement already satisfied: scipy>=1.1.0 in /usr/local/lib/python3.7/dist-packages (from skorch) (1.7.3)\n",
            "Requirement already satisfied: tabulate>=0.7.7 in /usr/local/lib/python3.7/dist-packages (from skorch) (0.8.10)\n",
            "Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=0.19.1->skorch) (1.1.0)\n",
            "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=0.19.1->skorch) (3.1.0)\n",
            "Installing collected packages: skorch\n",
            "Successfully installed skorch-0.11.0\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import torch\n",
        "from torch import nn\n",
        "import torch.nn.functional as F\n",
        "from sklearn.datasets import make_classification"
      ],
      "metadata": {
        "id": "eQyQ1jmyipFh"
      },
      "execution_count": 37,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "X, y = make_classification(2000, 10, random_state=0)\n",
        "X, y = X.astype(np.float32), y.astype(np.int64)"
      ],
      "metadata": {
        "id": "H7REHSWIlAF7"
      },
      "execution_count": 42,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "X.shape"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "l6_g5Mu-lG9k",
        "outputId": "a6d1b02c-c269-4e80-92f9-8ff738c15fee"
      },
      "execution_count": 43,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(2000, 10)"
            ]
          },
          "metadata": {},
          "execution_count": 43
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "y.shape"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "h1tzOCdPlIA2",
        "outputId": "0b9b5b46-9f5a-43f7-9d24-5b708c2fd3d1"
      },
      "execution_count": 44,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(2000,)"
            ]
          },
          "metadata": {},
          "execution_count": 44
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "class ClassifierModule(nn.Module):\n",
        "    def __init__(\n",
        "            self,\n",
        "            num_units=30,\n",
        "            nonlin=F.relu,\n",
        "            dropout=0.5,\n",
        "    ):\n",
        "        super(ClassifierModule, self).__init__()\n",
        "        self.num_units = num_units\n",
        "        self.nonlin = nonlin\n",
        "        self.dropout = dropout\n",
        "\n",
        "        self.dense0 = nn.Linear(10, num_units)\n",
        "        self.nonlin = nonlin\n",
        "        self.dropout = nn.Dropout(dropout)\n",
        "        self.dense1 = nn.Linear(num_units, 10)\n",
        "        self.output = nn.Linear(10, 2)\n",
        "\n",
        "    def forward(self, X, **kwargs):\n",
        "        X = self.nonlin(self.dense0(X))\n",
        "        X = self.dropout(X)\n",
        "        X = F.relu(self.dense1(X))\n",
        "        X = F.softmax(self.output(X), dim=-1)\n",
        "        return X"
      ],
      "metadata": {
        "id": "qNyosViHlJOE"
      },
      "execution_count": 45,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from skorch import NeuralNetClassifier"
      ],
      "metadata": {
        "id": "aWJO0zQdlvZY"
      },
      "execution_count": 46,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "net = NeuralNetClassifier(\n",
        "    ClassifierModule,\n",
        "    max_epochs=20,\n",
        "    lr=0.1,\n",
        "#     device='cuda',  # uncomment this to train with CUDA\n",
        ")"
      ],
      "metadata": {
        "id": "LaHlw55nlzFq"
      },
      "execution_count": 47,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "net.get_params()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "G3zQQCbul3ym",
        "outputId": "c0ab23f5-393c-4021-b536-f4b996e25829"
      },
      "execution_count": 48,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'module': __main__.ClassifierModule,\n",
              " 'criterion': torch.nn.modules.loss.NLLLoss,\n",
              " 'optimizer': torch.optim.sgd.SGD,\n",
              " 'lr': 0.1,\n",
              " 'max_epochs': 20,\n",
              " 'batch_size': 128,\n",
              " 'iterator_train': torch.utils.data.dataloader.DataLoader,\n",
              " 'iterator_valid': torch.utils.data.dataloader.DataLoader,\n",
              " 'dataset': skorch.dataset.Dataset,\n",
              " 'train_split': <skorch.dataset.ValidSplit object at 0x7f54811fcfd0>,\n",
              " 'callbacks': None,\n",
              " 'predict_nonlinearity': 'auto',\n",
              " 'warm_start': False,\n",
              " 'verbose': 1,\n",
              " 'device': 'cpu',\n",
              " '_kwargs': {},\n",
              " 'classes': None,\n",
              " 'callbacks__epoch_timer': <skorch.callbacks.logging.EpochTimer at 0x7f54811029d0>,\n",
              " 'callbacks__train_loss': <skorch.callbacks.scoring.PassthroughScoring at 0x7f5481022810>,\n",
              " 'callbacks__train_loss__name': 'train_loss',\n",
              " 'callbacks__train_loss__lower_is_better': True,\n",
              " 'callbacks__train_loss__on_train': True,\n",
              " 'callbacks__valid_loss': <skorch.callbacks.scoring.PassthroughScoring at 0x7f5481017d90>,\n",
              " 'callbacks__valid_loss__name': 'valid_loss',\n",
              " 'callbacks__valid_loss__lower_is_better': True,\n",
              " 'callbacks__valid_loss__on_train': False,\n",
              " 'callbacks__valid_acc': <skorch.callbacks.scoring.EpochScoring at 0x7f5481017dd0>,\n",
              " 'callbacks__valid_acc__scoring': 'accuracy',\n",
              " 'callbacks__valid_acc__lower_is_better': False,\n",
              " 'callbacks__valid_acc__on_train': False,\n",
              " 'callbacks__valid_acc__name': 'valid_acc',\n",
              " 'callbacks__valid_acc__target_extractor': <function skorch.utils.to_numpy(X)>,\n",
              " 'callbacks__valid_acc__use_caching': True,\n",
              " 'callbacks__print_log': <skorch.callbacks.logging.PrintLog at 0x7f54811e74d0>,\n",
              " 'callbacks__print_log__keys_ignored': None,\n",
              " 'callbacks__print_log__sink': <function print>,\n",
              " 'callbacks__print_log__tablefmt': 'simple',\n",
              " 'callbacks__print_log__floatfmt': '.4f',\n",
              " 'callbacks__print_log__stralign': 'right'}"
            ]
          },
          "metadata": {},
          "execution_count": 48
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "net.fit(X, y)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "CCtXMTEjmBIA",
        "outputId": "8eee0c4f-f238-49d9-d2be-ec43ed26673a"
      },
      "execution_count": 54,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "  epoch    train_loss    valid_acc    valid_loss     dur\n",
            "-------  ------------  -----------  ------------  ------\n",
            "      1        \u001b[36m0.7076\u001b[0m       \u001b[32m0.5325\u001b[0m        \u001b[35m0.6674\u001b[0m  0.0255\n",
            "      2        \u001b[36m0.6532\u001b[0m       \u001b[32m0.8050\u001b[0m        \u001b[35m0.5975\u001b[0m  0.0217\n",
            "      3        \u001b[36m0.5660\u001b[0m       \u001b[32m0.9675\u001b[0m        \u001b[35m0.4638\u001b[0m  0.0206\n",
            "      4        \u001b[36m0.4265\u001b[0m       \u001b[32m0.9800\u001b[0m        \u001b[35m0.2979\u001b[0m  0.0189\n",
            "      5        \u001b[36m0.2913\u001b[0m       \u001b[32m0.9875\u001b[0m        \u001b[35m0.1789\u001b[0m  0.0207\n",
            "      6        \u001b[36m0.2128\u001b[0m       \u001b[32m0.9925\u001b[0m        \u001b[35m0.1150\u001b[0m  0.0208\n",
            "      7        \u001b[36m0.1689\u001b[0m       0.9900        \u001b[35m0.0825\u001b[0m  0.0207\n",
            "      8        \u001b[36m0.1496\u001b[0m       0.9900        \u001b[35m0.0644\u001b[0m  0.0195\n",
            "      9        \u001b[36m0.1183\u001b[0m       0.9900        \u001b[35m0.0545\u001b[0m  0.0197\n",
            "     10        0.1218       0.9900        \u001b[35m0.0490\u001b[0m  0.0198\n",
            "     11        0.1240       0.9900        \u001b[35m0.0464\u001b[0m  0.0203\n",
            "     12        \u001b[36m0.1090\u001b[0m       0.9900        \u001b[35m0.0428\u001b[0m  0.0213\n",
            "     13        \u001b[36m0.1050\u001b[0m       0.9900        \u001b[35m0.0410\u001b[0m  0.0215\n",
            "     14        0.1067       0.9875        \u001b[35m0.0399\u001b[0m  0.0214\n",
            "     15        0.1072       0.9900        \u001b[35m0.0392\u001b[0m  0.0211\n",
            "     16        \u001b[36m0.0958\u001b[0m       0.9900        \u001b[35m0.0378\u001b[0m  0.0241\n",
            "     17        0.0964       0.9900        \u001b[35m0.0371\u001b[0m  0.0213\n",
            "     18        0.0986       0.9925        \u001b[35m0.0361\u001b[0m  0.0223\n",
            "     19        \u001b[36m0.0884\u001b[0m       0.9900        0.0363  0.0205\n",
            "     20        0.0991       0.9900        0.0366  0.0213\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<class 'skorch.classifier.NeuralNetClassifier'>[initialized](\n",
              "  module_=ClassifierModule(\n",
              "    (dense0): Linear(in_features=10, out_features=30, bias=True)\n",
              "    (dropout): Dropout(p=0.5, inplace=False)\n",
              "    (dense1): Linear(in_features=30, out_features=10, bias=True)\n",
              "    (output): Linear(in_features=10, out_features=2, bias=True)\n",
              "  ),\n",
              ")"
            ]
          },
          "metadata": {},
          "execution_count": 54
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "list(net.get_params())"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ltGgP7TcmbQn",
        "outputId": "d505b884-78c3-4922-e6f7-7614e8858b60"
      },
      "execution_count": 55,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['module',\n",
              " 'criterion',\n",
              " 'optimizer',\n",
              " 'lr',\n",
              " 'max_epochs',\n",
              " 'batch_size',\n",
              " 'iterator_train',\n",
              " 'iterator_valid',\n",
              " 'dataset',\n",
              " 'train_split',\n",
              " 'callbacks',\n",
              " 'predict_nonlinearity',\n",
              " 'warm_start',\n",
              " 'verbose',\n",
              " 'device',\n",
              " '_kwargs',\n",
              " 'classes',\n",
              " 'callbacks__epoch_timer',\n",
              " 'callbacks__train_loss',\n",
              " 'callbacks__train_loss__name',\n",
              " 'callbacks__train_loss__lower_is_better',\n",
              " 'callbacks__train_loss__on_train',\n",
              " 'callbacks__valid_loss',\n",
              " 'callbacks__valid_loss__name',\n",
              " 'callbacks__valid_loss__lower_is_better',\n",
              " 'callbacks__valid_loss__on_train',\n",
              " 'callbacks__valid_acc',\n",
              " 'callbacks__valid_acc__scoring',\n",
              " 'callbacks__valid_acc__lower_is_better',\n",
              " 'callbacks__valid_acc__on_train',\n",
              " 'callbacks__valid_acc__name',\n",
              " 'callbacks__valid_acc__target_extractor',\n",
              " 'callbacks__valid_acc__use_caching',\n",
              " 'callbacks__print_log',\n",
              " 'callbacks__print_log__keys_ignored',\n",
              " 'callbacks__print_log__sink',\n",
              " 'callbacks__print_log__tablefmt',\n",
              " 'callbacks__print_log__floatfmt',\n",
              " 'callbacks__print_log__stralign']"
            ]
          },
          "metadata": {},
          "execution_count": 55
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "y_pred = net.predict(X[:5])\n",
        "y_pred"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ei6h8zTkmh5M",
        "outputId": "ea80c697-8b60-4079-d4d3-eaa0e1cd673d"
      },
      "execution_count": 56,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([1, 0, 0, 1, 1])"
            ]
          },
          "metadata": {},
          "execution_count": 56
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "y_proba = net.predict_proba(X[:5])\n",
        "y_proba"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bcD-NWlsmmGl",
        "outputId": "4aed96f5-ff6e-4d9d-fed8-3a5205bf29e4"
      },
      "execution_count": 57,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[7.7738642e-04, 9.9922264e-01],\n",
              "       [9.9628782e-01, 3.7122301e-03],\n",
              "       [9.9648917e-01, 3.5108225e-03],\n",
              "       [3.2411060e-01, 6.7588937e-01],\n",
              "       [4.5940662e-03, 9.9540591e-01]], dtype=float32)"
            ]
          },
          "metadata": {},
          "execution_count": 57
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.datasets import make_regression"
      ],
      "metadata": {
        "id": "0Lr7Z7uempE7"
      },
      "execution_count": 58,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "X_regr, y_regr = make_regression(1000, 20, n_informative=10, random_state=0)\n",
        "X_regr = X_regr.astype(np.float32)\n",
        "y_regr = y_regr.astype(np.float32) / 100\n",
        "y_regr = y_regr.reshape(-1, 1)"
      ],
      "metadata": {
        "id": "BfeRNv8pm0t9"
      },
      "execution_count": 59,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "X_regr.shape, y_regr.shape, y_regr.min(), y_regr.max()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "g5YRnY_Wm3ue",
        "outputId": "e0344f14-eee6-40c1-e936-1138b1c355cb"
      },
      "execution_count": 60,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "((1000, 20), (1000, 1), -6.4901485, 6.154505)"
            ]
          },
          "metadata": {},
          "execution_count": 60
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "class RegressorModule(nn.Module):\n",
        "    def __init__(\n",
        "            self,\n",
        "            num_units=10,\n",
        "            nonlin=F.relu,\n",
        "    ):\n",
        "        super(RegressorModule, self).__init__()\n",
        "        self.num_units = num_units\n",
        "        self.nonlin = nonlin\n",
        "\n",
        "        self.dense0 = nn.Linear(20, num_units)\n",
        "        self.nonlin = nonlin\n",
        "        self.dense1 = nn.Linear(num_units, 10)\n",
        "        self.output = nn.Linear(10, 1)\n",
        "\n",
        "    def forward(self, X, **kwargs):\n",
        "        X = self.nonlin(self.dense0(X))\n",
        "        X = F.relu(self.dense1(X))\n",
        "        X = self.output(X)\n",
        "        return X"
      ],
      "metadata": {
        "id": "pUq4j3wfm60R"
      },
      "execution_count": 61,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from skorch import NeuralNetRegressor"
      ],
      "metadata": {
        "id": "-ie0SZWGm9is"
      },
      "execution_count": 62,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "net_regr = NeuralNetRegressor(\n",
        "    RegressorModule,\n",
        "    max_epochs=20,\n",
        "    lr=0.1,\n",
        "#     device='cuda',  # uncomment this to train with CUDA\n",
        ")"
      ],
      "metadata": {
        "id": "O1STCeN-nAZb"
      },
      "execution_count": 63,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "net_regr.fit(X_regr, y_regr)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "dbRlJQNOnDcS",
        "outputId": "659f24e3-8d8e-4741-eb47-4958e94a9e69"
      },
      "execution_count": 64,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "  epoch    train_loss    valid_loss     dur\n",
            "-------  ------------  ------------  ------\n",
            "      1        \u001b[36m4.3247\u001b[0m        \u001b[32m3.0078\u001b[0m  0.0170\n",
            "      2        \u001b[36m1.7262\u001b[0m        \u001b[32m0.6808\u001b[0m  0.0123\n",
            "      3        \u001b[36m0.6510\u001b[0m        \u001b[32m0.2147\u001b[0m  0.0115\n",
            "      4        \u001b[36m0.1811\u001b[0m        \u001b[32m0.2132\u001b[0m  0.0118\n",
            "      5        0.1906        \u001b[32m0.1127\u001b[0m  0.0108\n",
            "      6        \u001b[36m0.1143\u001b[0m        0.3361  0.0204\n",
            "      7        0.3835        \u001b[32m0.0899\u001b[0m  0.0113\n",
            "      8        \u001b[36m0.0845\u001b[0m        0.1574  0.0117\n",
            "      9        0.1099        \u001b[32m0.0486\u001b[0m  0.0130\n",
            "     10        \u001b[36m0.0485\u001b[0m        0.0974  0.0128\n",
            "     11        0.0907        \u001b[32m0.0447\u001b[0m  0.0108\n",
            "     12        \u001b[36m0.0481\u001b[0m        0.0947  0.0129\n",
            "     13        0.0881        \u001b[32m0.0322\u001b[0m  0.0128\n",
            "     14        \u001b[36m0.0323\u001b[0m        0.0599  0.0117\n",
            "     15        0.0461        \u001b[32m0.0180\u001b[0m  0.0115\n",
            "     16        \u001b[36m0.0161\u001b[0m        0.0328  0.0123\n",
            "     17        0.0231        \u001b[32m0.0125\u001b[0m  0.0123\n",
            "     18        \u001b[36m0.0098\u001b[0m        0.0208  0.0123\n",
            "     19        0.0143        \u001b[32m0.0102\u001b[0m  0.0112\n",
            "     20        \u001b[36m0.0074\u001b[0m        0.0153  0.0121\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<class 'skorch.regressor.NeuralNetRegressor'>[initialized](\n",
              "  module_=RegressorModule(\n",
              "    (dense0): Linear(in_features=20, out_features=10, bias=True)\n",
              "    (dense1): Linear(in_features=10, out_features=10, bias=True)\n",
              "    (output): Linear(in_features=10, out_features=1, bias=True)\n",
              "  ),\n",
              ")"
            ]
          },
          "metadata": {},
          "execution_count": 64
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "y_pred = net_regr.predict(X_regr[:5])\n",
        "y_pred"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4T7c2gi-nF2w",
        "outputId": "53a70186-d19e-4871-9b35-c392e293906d"
      },
      "execution_count": 65,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[ 0.7368696 ],\n",
              "       [-1.2884711 ],\n",
              "       [-0.51758516],\n",
              "       [-0.11890286],\n",
              "       [-0.61254007]], dtype=float32)"
            ]
          },
          "metadata": {},
          "execution_count": 65
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import pickle"
      ],
      "metadata": {
        "id": "acbQJhRnnIwh"
      },
      "execution_count": 66,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "file_name = '/tmp/mymodel.pkl'"
      ],
      "metadata": {
        "id": "zcHBv4RdnML6"
      },
      "execution_count": 67,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "with open(file_name, 'wb') as f:\n",
        "    pickle.dump(net, f)"
      ],
      "metadata": {
        "id": "icQ3N7Z3nO4g"
      },
      "execution_count": 68,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "with open(file_name, 'rb') as f:\n",
        "    new_net = pickle.load(f)"
      ],
      "metadata": {
        "id": "Gum-OYjTnRfw"
      },
      "execution_count": 69,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "net.save_params(f_params=file_name)  # a file handler also works"
      ],
      "metadata": {
        "id": "VhlAyzqRnUJA"
      },
      "execution_count": 70,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# first initialize the model\n",
        "new_net = NeuralNetClassifier(\n",
        "    ClassifierModule,\n",
        "    max_epochs=20,\n",
        "    lr=0.1,\n",
        ").initialize()"
      ],
      "metadata": {
        "id": "u78biAyXnXrJ"
      },
      "execution_count": 71,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "new_net.load_params(file_name)"
      ],
      "metadata": {
        "id": "x5JHxJUDnZ_l"
      },
      "execution_count": 72,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.pipeline import Pipeline\n",
        "from sklearn.preprocessing import StandardScaler"
      ],
      "metadata": {
        "id": "PXRcEtv5ncHj"
      },
      "execution_count": 73,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "pipe = Pipeline([\n",
        "    ('scale', StandardScaler()),\n",
        "    ('net', net),\n",
        "])"
      ],
      "metadata": {
        "id": "iVZZcS1snhMJ"
      },
      "execution_count": 74,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "pipe.fit(X, y)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7H26qKpVnjwX",
        "outputId": "b2f30361-e9aa-4b3e-e60a-e832eaf359c7"
      },
      "execution_count": 75,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Re-initializing module.\n",
            "Re-initializing criterion.\n",
            "Re-initializing optimizer.\n",
            "  epoch    train_loss    valid_acc    valid_loss     dur\n",
            "-------  ------------  -----------  ------------  ------\n",
            "      1        \u001b[36m0.6925\u001b[0m       \u001b[32m0.5250\u001b[0m        \u001b[35m0.6640\u001b[0m  0.0355\n",
            "      2        \u001b[36m0.6361\u001b[0m       \u001b[32m0.9075\u001b[0m        \u001b[35m0.5834\u001b[0m  0.0354\n",
            "      3        \u001b[36m0.5447\u001b[0m       \u001b[32m0.9550\u001b[0m        \u001b[35m0.4427\u001b[0m  0.0340\n",
            "      4        \u001b[36m0.4197\u001b[0m       \u001b[32m0.9675\u001b[0m        \u001b[35m0.2898\u001b[0m  0.0207\n",
            "      5        \u001b[36m0.3019\u001b[0m       \u001b[32m0.9775\u001b[0m        \u001b[35m0.1798\u001b[0m  0.0214\n",
            "      6        \u001b[36m0.2282\u001b[0m       \u001b[32m0.9825\u001b[0m        \u001b[35m0.1206\u001b[0m  0.0217\n",
            "      7        \u001b[36m0.1790\u001b[0m       \u001b[32m0.9875\u001b[0m        \u001b[35m0.0869\u001b[0m  0.0207\n",
            "      8        \u001b[36m0.1550\u001b[0m       0.9875        \u001b[35m0.0697\u001b[0m  0.0208\n",
            "      9        \u001b[36m0.1473\u001b[0m       0.9875        \u001b[35m0.0594\u001b[0m  0.0196\n",
            "     10        \u001b[36m0.1249\u001b[0m       0.9875        \u001b[35m0.0525\u001b[0m  0.0200\n",
            "     11        0.1294       \u001b[32m0.9900\u001b[0m        \u001b[35m0.0482\u001b[0m  0.0213\n",
            "     12        \u001b[36m0.1194\u001b[0m       \u001b[32m0.9925\u001b[0m        \u001b[35m0.0446\u001b[0m  0.0220\n",
            "     13        \u001b[36m0.1192\u001b[0m       \u001b[32m0.9950\u001b[0m        \u001b[35m0.0428\u001b[0m  0.0291\n",
            "     14        \u001b[36m0.1035\u001b[0m       0.9925        \u001b[35m0.0406\u001b[0m  0.0215\n",
            "     15        \u001b[36m0.0989\u001b[0m       0.9925        \u001b[35m0.0394\u001b[0m  0.0223\n",
            "     16        0.0999       0.9925        \u001b[35m0.0386\u001b[0m  0.0206\n",
            "     17        \u001b[36m0.0928\u001b[0m       0.9925        \u001b[35m0.0376\u001b[0m  0.0200\n",
            "     18        0.0980       0.9925        \u001b[35m0.0370\u001b[0m  0.0189\n",
            "     19        0.0969       0.9925        \u001b[35m0.0364\u001b[0m  0.0211\n",
            "     20        \u001b[36m0.0876\u001b[0m       0.9925        \u001b[35m0.0358\u001b[0m  0.0213\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Pipeline(steps=[('scale', StandardScaler()),\n",
              "                ('net',\n",
              "                 <class 'skorch.classifier.NeuralNetClassifier'>[initialized](\n",
              "  module_=ClassifierModule(\n",
              "    (dense0): Linear(in_features=10, out_features=30, bias=True)\n",
              "    (dropout): Dropout(p=0.5, inplace=False)\n",
              "    (dense1): Linear(in_features=30, out_features=10, bias=True)\n",
              "    (output): Linear(in_features=10, out_features=2, bias=True)\n",
              "  ),\n",
              "))])"
            ]
          },
          "metadata": {},
          "execution_count": 75
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "y_proba = pipe.predict_proba(X[:5])\n",
        "y_proba"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ceFzJBIonmj-",
        "outputId": "e2b76c8d-7bd2-44de-8752-a134eef6b3ed"
      },
      "execution_count": 76,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[0.00224374, 0.9977563 ],\n",
              "       [0.9986193 , 0.00138069],\n",
              "       [0.99899906, 0.00100095],\n",
              "       [0.30393705, 0.6960629 ],\n",
              "       [0.00816792, 0.9918321 ]], dtype=float32)"
            ]
          },
          "metadata": {},
          "execution_count": 76
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from skorch.callbacks import EpochScoring"
      ],
      "metadata": {
        "id": "3M5Gpusdnpyw"
      },
      "execution_count": 77,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "auc = EpochScoring(scoring='roc_auc', lower_is_better=False)"
      ],
      "metadata": {
        "id": "inDxc566nvjM"
      },
      "execution_count": 78,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "net = NeuralNetClassifier(\n",
        "    ClassifierModule,\n",
        "    max_epochs=20,\n",
        "    lr=0.1,\n",
        "    callbacks=[auc],\n",
        ")"
      ],
      "metadata": {
        "id": "n9Gg8KfhnyFm"
      },
      "execution_count": 79,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "net.fit(X, y)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5zAceIQUn0aC",
        "outputId": "ee8f068c-8308-4dd2-8eb1-4a62dfd697af"
      },
      "execution_count": 80,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "  epoch    roc_auc    train_loss    valid_acc    valid_loss     dur\n",
            "-------  ---------  ------------  -----------  ------------  ------\n",
            "      1     \u001b[36m0.9544\u001b[0m        \u001b[32m0.6614\u001b[0m       \u001b[35m0.8900\u001b[0m        \u001b[31m0.6294\u001b[0m  0.0193\n",
            "      2     \u001b[36m0.9845\u001b[0m        \u001b[32m0.5875\u001b[0m       \u001b[35m0.9625\u001b[0m        \u001b[31m0.5233\u001b[0m  0.0208\n",
            "      3     \u001b[36m0.9899\u001b[0m        \u001b[32m0.4798\u001b[0m       \u001b[35m0.9800\u001b[0m        \u001b[31m0.3647\u001b[0m  0.0198\n",
            "      4     \u001b[36m0.9945\u001b[0m        \u001b[32m0.3600\u001b[0m       \u001b[35m0.9825\u001b[0m        \u001b[31m0.2302\u001b[0m  0.0208\n",
            "      5     \u001b[36m0.9972\u001b[0m        \u001b[32m0.2682\u001b[0m       \u001b[35m0.9850\u001b[0m        \u001b[31m0.1451\u001b[0m  0.0200\n",
            "      6     \u001b[36m0.9975\u001b[0m        \u001b[32m0.2087\u001b[0m       0.9850        \u001b[31m0.1002\u001b[0m  0.0187\n",
            "      7     \u001b[36m0.9978\u001b[0m        \u001b[32m0.1869\u001b[0m       0.9850        \u001b[31m0.0762\u001b[0m  0.0191\n",
            "      8     \u001b[36m0.9979\u001b[0m        \u001b[32m0.1699\u001b[0m       0.9850        \u001b[31m0.0640\u001b[0m  0.0222\n",
            "      9     \u001b[36m0.9980\u001b[0m        \u001b[32m0.1430\u001b[0m       \u001b[35m0.9875\u001b[0m        \u001b[31m0.0567\u001b[0m  0.0200\n",
            "     10     \u001b[36m0.9981\u001b[0m        \u001b[32m0.1338\u001b[0m       0.9875        \u001b[31m0.0500\u001b[0m  0.0201\n",
            "     11     \u001b[36m0.9981\u001b[0m        \u001b[32m0.1214\u001b[0m       0.9875        \u001b[31m0.0464\u001b[0m  0.0355\n",
            "     12     0.9981        \u001b[32m0.1167\u001b[0m       \u001b[35m0.9900\u001b[0m        \u001b[31m0.0442\u001b[0m  0.0346\n",
            "     13     \u001b[36m0.9982\u001b[0m        \u001b[32m0.1072\u001b[0m       0.9875        \u001b[31m0.0419\u001b[0m  0.0396\n",
            "     14     0.9981        0.1152       0.9900        \u001b[31m0.0404\u001b[0m  0.0337\n",
            "     15     0.9981        0.1086       0.9900        \u001b[31m0.0395\u001b[0m  0.0341\n",
            "     16     0.9982        \u001b[32m0.0905\u001b[0m       0.9875        \u001b[31m0.0387\u001b[0m  0.0338\n",
            "     17     0.9981        0.0983       0.9875        \u001b[31m0.0382\u001b[0m  0.0466\n",
            "     18     0.9981        0.0929       0.9875        \u001b[31m0.0373\u001b[0m  0.0618\n",
            "     19     0.9982        0.1009       0.9875        \u001b[31m0.0368\u001b[0m  0.0302\n",
            "     20     \u001b[36m0.9982\u001b[0m        0.0981       0.9875        \u001b[31m0.0362\u001b[0m  0.0219\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<class 'skorch.classifier.NeuralNetClassifier'>[initialized](\n",
              "  module_=ClassifierModule(\n",
              "    (dense0): Linear(in_features=10, out_features=30, bias=True)\n",
              "    (dropout): Dropout(p=0.5, inplace=False)\n",
              "    (dense1): Linear(in_features=30, out_features=10, bias=True)\n",
              "    (output): Linear(in_features=10, out_features=2, bias=True)\n",
              "  ),\n",
              ")"
            ]
          },
          "metadata": {},
          "execution_count": 80
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(', '.join(net.prefixes_))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "C2APH3i2n3Ak",
        "outputId": "8ddddd6f-d573-4589-adf8-145d359cf0b4"
      },
      "execution_count": 81,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "iterator_train, iterator_valid, callbacks, dataset, module, criterion, optimizer\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.model_selection import GridSearchCV"
      ],
      "metadata": {
        "id": "y5tvThpUn9nv"
      },
      "execution_count": 82,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "net = NeuralNetClassifier(\n",
        "    ClassifierModule,\n",
        "    max_epochs=20,\n",
        "    lr=0.1,\n",
        "    optimizer__momentum=0.9,\n",
        "    verbose=0,\n",
        "    train_split=False,\n",
        ")"
      ],
      "metadata": {
        "id": "IYtVyJEooA3k"
      },
      "execution_count": 83,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "params = {\n",
        "    'lr': [0.05, 0.1],\n",
        "    'module__num_units': [10, 20],\n",
        "    'module__dropout': [0, 0.5],\n",
        "    'optimizer__nesterov': [False, True],\n",
        "}"
      ],
      "metadata": {
        "id": "UyB_VAegoDTO"
      },
      "execution_count": 84,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "gs = GridSearchCV(net, params, refit=False, cv=3, scoring='accuracy', verbose=2)"
      ],
      "metadata": {
        "id": "-pOU64HpoF9m"
      },
      "execution_count": 85,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "gs.fit(X, y)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1QKvoeBOoI8V",
        "outputId": "6d39f9d9-22d7-4e3b-cf84-4bcb5d73b198"
      },
      "execution_count": 86,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Fitting 3 folds for each of 16 candidates, totalling 48 fits\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=10, optimizer__nesterov=False; total time=   0.4s\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=10, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=10, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=10, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=10, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=10, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.05, module__dropout=0.5, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=10, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=10, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=10, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=10, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=10, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=10, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=10, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=20, optimizer__nesterov=False; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n",
            "[CV] END lr=0.1, module__dropout=0.5, module__num_units=20, optimizer__nesterov=True; total time=   0.3s\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "GridSearchCV(cv=3,\n",
              "             estimator=<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](\n",
              "  module=<class '__main__.ClassifierModule'>,\n",
              "),\n",
              "             param_grid={'lr': [0.05, 0.1], 'module__dropout': [0, 0.5],\n",
              "                         'module__num_units': [10, 20],\n",
              "                         'optimizer__nesterov': [False, True]},\n",
              "             refit=False, scoring='accuracy', verbose=2)"
            ]
          },
          "metadata": {},
          "execution_count": 86
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(gs.best_score_, gs.best_params_)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0GpGn-3KoLH2",
        "outputId": "5c3e97a4-0baf-4fb1-98f3-fb0a6dbc3153"
      },
      "execution_count": 87,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "0.988499744121933 {'lr': 0.1, 'module__dropout': 0.5, 'module__num_units': 20, 'optimizer__nesterov': False}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "net"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "45Pp2ToJoPuR",
        "outputId": "d93dfe90-60d7-4b90-8846-bbdac658ae2f"
      },
      "execution_count": 88,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](\n",
              "  module=<class '__main__.ClassifierModule'>,\n",
              ")"
            ]
          },
          "metadata": {},
          "execution_count": 88
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#conda install captum -c pytorch\n",
        "#pip install captum"
      ],
      "metadata": {
        "id": "oVTIBNK6DMBw"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "pip install captum"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "VNnW29SGuCii",
        "outputId": "1b835c6e-3c06-4209-a4b2-10fd98f9e047"
      },
      "execution_count": 95,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Collecting captum\n",
            "  Downloading captum-0.5.0-py3-none-any.whl (1.4 MB)\n",
            "\u001b[?25l\r\u001b[K     |▎                               | 10 kB 33.3 MB/s eta 0:00:01\r\u001b[K     |▌                               | 20 kB 42.7 MB/s eta 0:00:01\r\u001b[K     |▊                               | 30 kB 50.0 MB/s eta 0:00:01\r\u001b[K     |█                               | 40 kB 35.9 MB/s eta 0:00:01\r\u001b[K     |█▏                              | 51 kB 29.3 MB/s eta 0:00:01\r\u001b[K     |█▍                              | 61 kB 33.6 MB/s eta 0:00:01\r\u001b[K     |█▋                              | 71 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█▉                              | 81 kB 28.7 MB/s eta 0:00:01\r\u001b[K     |██                              | 92 kB 30.9 MB/s eta 0:00:01\r\u001b[K     |██▎                             | 102 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██▌                             | 112 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██▊                             | 122 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███                             | 133 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███▏                            | 143 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███▌                            | 153 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███▊                            | 163 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████                            | 174 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████▏                           | 184 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████▍                           | 194 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████▋                           | 204 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████▉                           | 215 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████                           | 225 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████▎                          | 235 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████▌                          | 245 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████▊                          | 256 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████                          | 266 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████▏                         | 276 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████▍                         | 286 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████▋                         | 296 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████                         | 307 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████▏                        | 317 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████▍                        | 327 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████▋                        | 337 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████▉                        | 348 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████                        | 358 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████▎                       | 368 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████▌                       | 378 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████▊                       | 389 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████                       | 399 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████▏                      | 409 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████▍                      | 419 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████▋                      | 430 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████▉                      | 440 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████▏                     | 450 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████▍                     | 460 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████▋                     | 471 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████▉                     | 481 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████                     | 491 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████▎                    | 501 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████▌                    | 512 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████▊                    | 522 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████                    | 532 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████▏                   | 542 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████▍                   | 552 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████▋                   | 563 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████▉                   | 573 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████                   | 583 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████▎                  | 593 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████▋                  | 604 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████▉                  | 614 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████                  | 624 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████▎                 | 634 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████▌                 | 645 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████▊                 | 655 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████                 | 665 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████▏                | 675 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████▍                | 686 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████▋                | 696 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████▉                | 706 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████                | 716 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████▎               | 727 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████▌               | 737 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████▊               | 747 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████               | 757 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████▎              | 768 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████▌              | 778 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████▊              | 788 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████              | 798 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████▏             | 808 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████▍             | 819 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████▋             | 829 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████▉             | 839 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████             | 849 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████▎            | 860 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████▌            | 870 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████▊            | 880 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████            | 890 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████▎           | 901 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████▌           | 911 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████▊           | 921 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████           | 931 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████▏          | 942 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████▍          | 952 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████▋          | 962 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████▉          | 972 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████          | 983 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████▎         | 993 kB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████▌         | 1.0 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████▊         | 1.0 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████         | 1.0 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████▏        | 1.0 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████▍        | 1.0 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████▊        | 1.1 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████        | 1.1 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████▏       | 1.1 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████▍       | 1.1 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████▋       | 1.1 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████▉       | 1.1 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████████       | 1.1 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████████▎      | 1.1 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████████▌      | 1.1 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████████▊      | 1.1 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████████      | 1.2 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████████▏     | 1.2 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████████▍     | 1.2 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████████▋     | 1.2 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████████     | 1.2 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████████▏    | 1.2 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████████▍    | 1.2 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████████▋    | 1.2 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████████▉    | 1.2 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████████    | 1.2 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████████▎   | 1.3 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████████▌   | 1.3 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████████▊   | 1.3 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████████████   | 1.3 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████████████▏  | 1.3 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████████████▍  | 1.3 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████████████▋  | 1.3 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████████████▉  | 1.3 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████████████  | 1.3 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████████████▍ | 1.4 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████████████▋ | 1.4 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████████████▉ | 1.4 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████████████ | 1.4 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████████████▎| 1.4 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████████████▌| 1.4 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████████████▊| 1.4 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████████████| 1.4 MB 29.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████████████| 1.4 MB 29.2 MB/s \n",
            "\u001b[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from captum) (1.21.6)\n",
            "Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from captum) (3.2.2)\n",
            "Requirement already satisfied: torch>=1.6 in /usr/local/lib/python3.7/dist-packages (from captum) (1.12.1+cu113)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch>=1.6->captum) (4.1.1)\n",
            "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->captum) (2.8.2)\n",
            "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->captum) (3.0.9)\n",
            "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->captum) (0.11.0)\n",
            "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->captum) (1.4.4)\n",
            "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.1->matplotlib->captum) (1.15.0)\n",
            "Installing collected packages: captum\n",
            "Successfully installed captum-0.5.0\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Initial imports\n",
        "import numpy as np\n",
        "\n",
        "import torch\n",
        "\n",
        "from captum.attr import IntegratedGradients\n",
        "from captum.attr import LayerConductance\n",
        "from captum.attr import NeuronConductance\n",
        "\n",
        "import matplotlib\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline\n",
        "\n",
        "from scipy import stats\n",
        "import pandas as pd"
      ],
      "metadata": {
        "id": "i4X87XMEuxAf"
      },
      "execution_count": 134,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "dataset_path = \"https://raw.githubusercontent.com/pradmishra1/PublicDatasets/main/titanic.csv\""
      ],
      "metadata": {
        "id": "KJUHsX5YvGD_"
      },
      "execution_count": 135,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "titanic_data = pd.read_csv(dataset_path)"
      ],
      "metadata": {
        "id": "UMvEnrWavefd"
      },
      "execution_count": 136,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "titanic_data.columns"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jToOuu3gvhXw",
        "outputId": "275ec11c-655c-49e9-f102-1c42a0f0b56b"
      },
      "execution_count": 137,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['Unnamed: 0', 'PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age',\n",
              "       'SibSp', 'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {},
          "execution_count": 137
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "del titanic_data['Unnamed: 0']"
      ],
      "metadata": {
        "id": "JVtuZRz7vmI2"
      },
      "execution_count": 138,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "del titanic_data['PassengerId']"
      ],
      "metadata": {
        "id": "fntW5Tcly1np"
      },
      "execution_count": 139,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "titanic_data.head()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "id": "w5vw1s1cvrrS",
        "outputId": "c222d82c-54ad-4fbd-c167-740c14fb01dc"
      },
      "execution_count": 140,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "   Survived  Pclass                                               Name  \\\n",
              "0         0       3                            Braund, Mr. Owen Harris   \n",
              "1         1       1  Cumings, Mrs. John Bradley (Florence Briggs Th...   \n",
              "2         1       3                             Heikkinen, Miss. Laina   \n",
              "3         1       1       Futrelle, Mrs. Jacques Heath (Lily May Peel)   \n",
              "4         0       3                           Allen, Mr. William Henry   \n",
              "\n",
              "      Sex   Age  SibSp  Parch            Ticket     Fare Cabin Embarked  \n",
              "0    male  22.0      1      0         A/5 21171   7.2500   NaN        S  \n",
              "1  female  38.0      1      0          PC 17599  71.2833   C85        C  \n",
              "2  female  26.0      0      0  STON/O2. 3101282   7.9250   NaN        S  \n",
              "3  female  35.0      1      0            113803  53.1000  C123        S  \n",
              "4    male  35.0      0      0            373450   8.0500   NaN        S  "
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-7e0e0119-7163-4f9c-a236-fa5532fe463c\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Survived</th>\n",
              "      <th>Pclass</th>\n",
              "      <th>Name</th>\n",
              "      <th>Sex</th>\n",
              "      <th>Age</th>\n",
              "      <th>SibSp</th>\n",
              "      <th>Parch</th>\n",
              "      <th>Ticket</th>\n",
              "      <th>Fare</th>\n",
              "      <th>Cabin</th>\n",
              "      <th>Embarked</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>Braund, Mr. Owen Harris</td>\n",
              "      <td>male</td>\n",
              "      <td>22.0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>A/5 21171</td>\n",
              "      <td>7.2500</td>\n",
              "      <td>NaN</td>\n",
              "      <td>S</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
              "      <td>female</td>\n",
              "      <td>38.0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>PC 17599</td>\n",
              "      <td>71.2833</td>\n",
              "      <td>C85</td>\n",
              "      <td>C</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>1</td>\n",
              "      <td>3</td>\n",
              "      <td>Heikkinen, Miss. Laina</td>\n",
              "      <td>female</td>\n",
              "      <td>26.0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>STON/O2. 3101282</td>\n",
              "      <td>7.9250</td>\n",
              "      <td>NaN</td>\n",
              "      <td>S</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
              "      <td>female</td>\n",
              "      <td>35.0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>113803</td>\n",
              "      <td>53.1000</td>\n",
              "      <td>C123</td>\n",
              "      <td>S</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>Allen, Mr. William Henry</td>\n",
              "      <td>male</td>\n",
              "      <td>35.0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>373450</td>\n",
              "      <td>8.0500</td>\n",
              "      <td>NaN</td>\n",
              "      <td>S</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-7e0e0119-7163-4f9c-a236-fa5532fe463c')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-7e0e0119-7163-4f9c-a236-fa5532fe463c button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-7e0e0119-7163-4f9c-a236-fa5532fe463c');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 140
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "titanic_data = pd.concat([titanic_data,\n",
        "                          pd.get_dummies(titanic_data['Sex']),\n",
        "                          pd.get_dummies(titanic_data['Embarked'],prefix=\"embark\"),\n",
        "                          pd.get_dummies(titanic_data['Pclass'],prefix=\"class\")], axis=1)\n",
        "titanic_data[\"Age\"] = titanic_data[\"Age\"].fillna(titanic_data[\"Age\"].mean())\n",
        "titanic_data[\"Fare\"] = titanic_data[\"Fare\"].fillna(titanic_data[\"Fare\"].mean())\n",
        "titanic_data = titanic_data.drop(['Name','Ticket','Cabin','Sex','Embarked','Pclass'], axis=1)"
      ],
      "metadata": {
        "id": "22Nh_5WRvuHx"
      },
      "execution_count": 141,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Set random seed for reproducibility.\n",
        "np.random.seed(707)\n",
        "\n",
        "# Convert features and labels to numpy arrays.\n",
        "labels = titanic_data[\"Survived\"].to_numpy()\n",
        "titanic_data = titanic_data.drop(['Survived'], axis=1)\n",
        "feature_names = list(titanic_data.columns)\n",
        "data = titanic_data.to_numpy()\n",
        "\n",
        "# Separate training and test sets using \n",
        "train_indices = np.random.choice(len(labels), int(0.7*len(labels)), replace=False)\n",
        "test_indices = list(set(range(len(labels))) - set(train_indices))\n",
        "train_features = data[train_indices]\n",
        "train_labels = labels[train_indices]\n",
        "test_features = data[test_indices]\n",
        "test_labels = labels[test_indices]"
      ],
      "metadata": {
        "id": "b3lonraBvy1K"
      },
      "execution_count": 142,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "train_features.shape"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RN4YB-RhxD7D",
        "outputId": "995c7e10-967c-4578-f713-809c59dcf6f3"
      },
      "execution_count": 143,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(623, 12)"
            ]
          },
          "metadata": {},
          "execution_count": 143
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import torch\n",
        "import torch.nn as nn\n",
        "torch.manual_seed(1)  # Set seed for reproducibility.\n",
        "class TitanicSimpleNNModel(nn.Module):\n",
        "    def __init__(self):\n",
        "        super().__init__()\n",
        "        self.linear1 = nn.Linear(12, 12)\n",
        "        self.sigmoid1 = nn.Sigmoid()\n",
        "        self.linear2 = nn.Linear(12, 8)\n",
        "        self.sigmoid2 = nn.Sigmoid()\n",
        "        self.linear3 = nn.Linear(8, 2)\n",
        "        self.softmax = nn.Softmax(dim=1)\n",
        "\n",
        "    def forward(self, x):\n",
        "        lin1_out = self.linear1(x)\n",
        "        sigmoid_out1 = self.sigmoid1(lin1_out)\n",
        "        sigmoid_out2 = self.sigmoid2(self.linear2(sigmoid_out1))\n",
        "        return self.softmax(self.linear3(sigmoid_out2))"
      ],
      "metadata": {
        "id": "Ig42Gaz_wPvn"
      },
      "execution_count": 145,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "net = TitanicSimpleNNModel()\n",
        "criterion = nn.CrossEntropyLoss()\n",
        "num_epochs = 200\n",
        "\n",
        "optimizer = torch.optim.Adam(net.parameters(), lr=0.1)\n",
        "input_tensor = torch.from_numpy(train_features).type(torch.FloatTensor)\n",
        "label_tensor = torch.from_numpy(train_labels)\n"
      ],
      "metadata": {
        "id": "KJR2degzwXQb"
      },
      "execution_count": 146,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "for epoch in range(num_epochs):\n",
        "  output = net(input_tensor)\n",
        "  loss = criterion(output, label_tensor)\n",
        "  optimizer.zero_grad()\n",
        "  loss.backward()\n",
        "  optimizer.step()\n",
        "  if epoch % 20 == 0:\n",
        "        print ('Epoch {}/{} => Loss: {:.2f}'.format(epoch+1, num_epochs, loss.item()))\n",
        "torch.save(net.state_dict(), '/model.pt')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "USpAuTdiwjXe",
        "outputId": "1206aeae-326d-49fe-ce7c-4014038969eb"
      },
      "execution_count": 147,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Epoch 1/200 => Loss: 0.70\n",
            "Epoch 21/200 => Loss: 0.55\n",
            "Epoch 41/200 => Loss: 0.50\n",
            "Epoch 61/200 => Loss: 0.49\n",
            "Epoch 81/200 => Loss: 0.48\n",
            "Epoch 101/200 => Loss: 0.49\n",
            "Epoch 121/200 => Loss: 0.47\n",
            "Epoch 141/200 => Loss: 0.47\n",
            "Epoch 161/200 => Loss: 0.47\n",
            "Epoch 181/200 => Loss: 0.47\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "out_probs = net(input_tensor).detach().numpy()\n",
        "out_classes = np.argmax(out_probs, axis=1)\n",
        "print(\"Train Accuracy:\", sum(out_classes == train_labels) / len(train_labels))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "6HITYSN3wy24",
        "outputId": "847b2200-c87a-43c6-9638-f1f1b189c07e"
      },
      "execution_count": 148,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Train Accuracy: 0.8523274478330658\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "test_input_tensor = torch.from_numpy(test_features).type(torch.FloatTensor)\n",
        "out_probs = net(test_input_tensor).detach().numpy()\n",
        "out_classes = np.argmax(out_probs, axis=1)\n",
        "print(\"Test Accuracy:\", sum(out_classes == test_labels) / len(test_labels))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TwVks4YAxgpK",
        "outputId": "57d35211-f3cf-47e7-eae9-1c388026f2d7"
      },
      "execution_count": 149,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Test Accuracy: 0.832089552238806\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "ig = IntegratedGradients(net)"
      ],
      "metadata": {
        "id": "0gk_6EsDxj97"
      },
      "execution_count": 150,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "test_input_tensor.requires_grad_()\n",
        "attr, delta = ig.attribute(test_input_tensor,target=1, return_convergence_delta=True)\n",
        "attr = attr.detach().numpy()"
      ],
      "metadata": {
        "id": "e5JQdtYMxn_P"
      },
      "execution_count": 151,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "np.round(attr,2)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pAjTFdZeyUUA",
        "outputId": "877b0fea-31d1-40d8-9568-eab033698f46"
      },
      "execution_count": 152,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[-0.7 ,  0.09, -0.  , ...,  0.  ,  0.  , -0.33],\n",
              "       [-2.78, -0.  , -0.  , ...,  0.  ,  0.  , -1.82],\n",
              "       [-0.65,  0.  , -0.  , ...,  0.  ,  0.  , -0.31],\n",
              "       ...,\n",
              "       [-0.47, -0.  , -0.  , ...,  0.71,  0.  , -0.  ],\n",
              "       [-0.1 , -0.  , -0.  , ...,  0.  ,  0.  , -0.1 ],\n",
              "       [-0.7 ,  0.  , -0.  , ...,  0.  ,  0.  , -0.28]])"
            ]
          },
          "metadata": {},
          "execution_count": 152
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "importances = np.mean(attr, axis=0)"
      ],
      "metadata": {
        "id": "M77MJ5Duydmr"
      },
      "execution_count": 153,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "for i in range(len(feature_names)):\n",
        "        print(feature_names[i], \": \", '%.3f'%(importances[i]))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "VUwp5gv2xslf",
        "outputId": "2c98b9cc-089d-4e78-80ca-b89f36378283"
      },
      "execution_count": 154,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Age :  -0.574\n",
            "SibSp :  -0.010\n",
            "Parch :  -0.026\n",
            "Fare :  0.278\n",
            "female :  0.101\n",
            "male :  -0.460\n",
            "embark_C :  0.042\n",
            "embark_Q :  0.005\n",
            "embark_S :  -0.021\n",
            "class_1 :  0.067\n",
            "class_2 :  0.090\n",
            "class_3 :  -0.144\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "cond = LayerConductance(net, net.sigmoid1)"
      ],
      "metadata": {
        "id": "9kmqqSyzx9lz"
      },
      "execution_count": 155,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "cond_vals = cond.attribute(test_input_tensor,target=1)\n",
        "cond_vals = cond_vals.detach().numpy()"
      ],
      "metadata": {
        "id": "55Ama-xlzW5-"
      },
      "execution_count": 156,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "Average_Neuron_Importances = np.mean(cond_vals, axis=0)\n",
        "Average_Neuron_Importances"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "buREteRfzaFa",
        "outputId": "dc4c9af8-9cb3-4b55-826c-02fea3bce835"
      },
      "execution_count": 158,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([ 0.03051018, -0.23244175,  0.04743345,  0.02102091, -0.08071412,\n",
              "       -0.09040915, -0.13398956, -0.04666219,  0.03577907, -0.07206058,\n",
              "       -0.15658873,  0.03491106], dtype=float32)"
            ]
          },
          "metadata": {},
          "execution_count": 158
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "neuron_cond = NeuronConductance(net, net.sigmoid1)"
      ],
      "metadata": {
        "id": "ES7XSuaZzfwN"
      },
      "execution_count": 159,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "neuron_cond_vals_10 = neuron_cond.attribute(test_input_tensor, neuron_selector=10, target=1)"
      ],
      "metadata": {
        "id": "Uwt8jwfCz7Np"
      },
      "execution_count": 160,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "neuron_cond_vals_0 = neuron_cond.attribute(test_input_tensor, neuron_selector=0, target=1)"
      ],
      "metadata": {
        "id": "OADTwr0hz-r5"
      },
      "execution_count": 161,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Average Feature Importances for Neuron 0\n",
        "nn0 = neuron_cond_vals_0.mean(dim=0).detach().numpy()\n",
        "np.round(nn0,3)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FDF6ys2A0CbR",
        "outputId": "18240c30-f09f-47bd-d674-34e348b72535"
      },
      "execution_count": 165,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([ 0.008,  0.   ,  0.   ,  0.028,  0.   , -0.004, -0.   ,  0.   ,\n",
              "       -0.001, -0.   ,  0.   , -0.   ], dtype=float32)"
            ]
          },
          "metadata": {},
          "execution_count": 165
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "h3qnyu7z0GTj"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}